<?php
/**
 * This file is part of won-uploader.
 *
 * Author: Daniel K.
 *
 * won-uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * won-uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with won-uploader. If not, see <http://www.gnu.org/licenses/>.
 */
   
 if( !defined("won-uploader")) die();    // Silence is golden
 
 $languages = $_language->listLanguages();
 
 $message = "";
 
 /* Check sent values */
 if( isset($_POST['sent']) && isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['password']) && !empty($_POST['password']) )
 {
 	try
 	{
 		Security::login($_POST['username'],$_POST['password']);
 		header("Location: ".$_paths->getHTTPURL());
 		die();
 	}
 	catch( Exception $ex )
 	{
 		$message = $_language->get("login-failed");
 	}
 }
 
?>
<!DOCTYPE html>
<html>
	<head>
		<title><?=$_language->get("login-title")?> | World of Nexus Uploader</title>
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
		<meta http-equiv="cache-control" content="no-cache" />
		<link rel="stylesheet" type="text/css" href="<?=$_paths->getHTTPAppDirectory()?>css/window.css" />
		<link rel="stylesheet" type="text/css" href="<?=$_paths->getHTTPAppDirectory()?>css/login.css" />
		<script type="text/javascript" src="<?=$_paths->getHTTPAppDirectory()?>js/login.js"></script>
		<!-- JQuery -->
		<link type="text/css" href="<?=$_paths->getHTTPAppDirectory()?>css/black-tie/jquery-ui-1.8.22.custom.css" rel="stylesheet" />
		<link href="http://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="<?=$_paths->getHTTPAppDirectory()?>js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="<?=$_paths->getHTTPAppDirectory()?>js/jquery-ui-1.8.22.custom.min.js"></script>
	</head>
	<body>
		<div id="wonu-topbar">
			<ul>
			<?php
				foreach( $languages as $lcode )
				{
					echo "<li><a href=\"".$_paths->getHTTPAppDirectory()."?l=$lcode\"><img src=\"".$_paths->getHTTPAppDirectory()."css/languages/$lcode.png\" alt=\"$lcode\" /></a></li>";
				}
			?>
			</ul>
		</div>
		<div id="wonu-alignment">
			<div id="wonu-login-main">
				<div id="wonu-login-titleimage">
					<img src="css/text.png" alt="World of Nexus" />
				</div>
				<div id="wonu-login">
					<form id="loginform" method="post" action="<?=$_paths->getHTTPAppDirectory()?>">
						<div id="wonu-login-content">
							<table>
								<tbody>
									<tr>
										<td id="wonu-login-td-username">
											<p>
												<?=$_language->get("username")?>:
											</p>
										</td>
										<td id="wonu-login-td-username-input">
											<input type="text" id="username" name="username" />
										</td>
									</tr>
									<tr>
										<td id="wonu-login-td-password">
											<p>
												<?=$_language->get("password")?>:
											</p>
										</td>
										<td id="wonu-login-td-password-input">
											<input type="password" id="password" name="password" />
										</td>
									</tr>
								</tbody>
							</table>
						</div>
						<div id="wonu-loginbar">
							<input type="submit" value="<?=$_language->get("login-submit")?>" name="sent" />
						</div>
					</form>
				</div>
			</div>
		</div>
		<div id="error-dialog" title="<?=$_language->get("login-dialog-title")?>">
			<p>
				<?=$_language->get("login-dialog-text")?>
			</p>
		</div>
		<?php
			if( $message != "" )
			{
				?>
				<div id="login-dialog" title="<?=$_language->get("login-dialog-title")?>">
					<p>
						<?=$message?>
					</p>
				</div>
				<?php
			} 
		?>
		<script>
			$( function() {
				initLogin();

				$("#error-dialog").dialog(
					{
						modal: true,
						resizeable: false,
						moveable: false,
						autoOpen: false,
						buttons: {
								"<?=$_language->get("okay")?>":function() { $("#error-dialog").dialog("close"); }
								} 
					});
				<?php
					if( $message != "" )
					{
						?>
						$("#login-dialog").dialog(
							{
								modal: true,
								resizeable: false,
								moveable: false,
								autoOpen: true,
								buttons: {
										"<?=$_language->get("okay")?>":function() { $("#login-dialog").dialog("close"); }
										} 
							});
						<?php
					} 
				?>
				
			});
		</script>
	</body>
</html>